//
//  MasterViewController.m
//  五角星
//
//  Created by situ_ios_004 on 15/9/24.
//  Copyright (c) 2015年 situ_ios_004. All rights reserved.
//

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "xing.h"
@interface MasterViewController ()

@property NSMutableArray *objects;
@end

@implementation MasterViewController

- (void)awakeFromNib {
    [super awakeFromNib];
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        self.clearsSelectionOnViewWillAppear = NO;
        self.preferredContentSize = CGSizeMake(320.0, 600.0);
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
    self.navigationItem.rightBarButtonItem = addButton;
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    
    
    xing *view=[[xing alloc]initWithFrame:[UIScreen mainScreen].bounds];
    view.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:view];
    
    
    UIImage *image=[self drawImageAtImageContext];
         UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
    imageView.center=CGPointMake(160, 230);
    CGFloat angle=2*M_PI/6.0;
    for (int i=1; i<6; i++) {
    
    imageView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);

    }
    [self.view addSubview:imageView];
}
#pragma mark 利用位图上下文添加水印效果
-(UIImage *)drawImageAtImageContext{
    //获得一个位图图形上下文
    CGSize size=CGSizeMake(300, 400);//画布大小
    UIGraphicsBeginImageContext(size);
        //添加水印
    CGContextRef context=UIGraphicsGetCurrentContext();
    [[UIColor redColor]setStroke];
    //CGContextRotateCTM(context, M_PI_4/4);
    NSArray* aa=[NSArray arrayWithObjects:@"青",@"岛",@"市",@"城",@"阳",@"区",nil];
    CGPoint centerPoint=CGPointMake(160, 230);
    CGFloat radius=110.0;
    CGPoint p1=CGPointMake(centerPoint.x, centerPoint.y-radius);
    CGContextMoveToPoint(context, p1.x, p1.y);
    CGFloat angle=2*M_PI/6.0;
    for (int i=0; i<=5; i++) {
        CGFloat x=centerPoint.x-sinf((i-2)*angle)*radius;
        CGFloat y=centerPoint.y-cosf((i-2)*angle)*radius;
        
        CGRect rect= CGRectMake(x, y, 300, 400);
        UIFont *font=[UIFont systemFontOfSize:20];//设置字体
        UIColor *color=[UIColor blueColor];//字体颜色
        NSMutableParagraphStyle *style=[[NSMutableParagraphStyle alloc]init];//段落样式
        NSTextAlignment align= NSTextAlignmentJustified;//对齐方式
        style.alignment=align;
        NSString *bb=[aa objectAtIndex:i];
        
        [bb  drawInRect:rect withAttributes:@{NSFontAttributeName:font,NSForegroundColorAttributeName:color,NSParagraphStyleAttributeName:style}];
    }
    
    //返回绘制的新图形
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    
    //最后一定不要忘记关闭对应的上下文
    UIGraphicsEndImageContext();
   
    return newImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)insertNewObject:(id)sender {
    if (!self.objects) {
        self.objects = [[NSMutableArray alloc] init];
    }
    [self.objects insertObject:[NSDate date] atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - Segues

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSDate *object = self.objects[indexPath.row];
        DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
        [controller setDetailItem:object];
        controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
        controller.navigationItem.leftItemsSupplementBackButton = YES;
    }
}

#pragma mark - Table View

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.objects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

    NSDate *object = self.objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.objects removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

@end
